package com.gitee.wsl.compose.ui.table.filter

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.InputChip
import androidx.compose.material3.InputChipDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.ui.table.model.CheckboxColumnSpec
import com.gitee.wsl.compose.ui.table.model.ColumnSpec
import com.gitee.wsl.compose.ui.table.model.DateColumnSpec
import com.gitee.wsl.compose.ui.table.model.DoubleColumnSpec
import com.gitee.wsl.compose.ui.table.model.DropdownColumnSpec
import com.gitee.wsl.compose.ui.table.model.IntColumnSpec
import com.gitee.wsl.compose.ui.table.model.TextColumnSpec
import com.gitee.wsl.compose.ui.table.icons.Close
import com.gitee.wsl.compose.ui.table.icons.DataTableIcons
import com.gitee.wsl.compose.ui.table.icons.FilterList

@Composable
internal fun <T> FilterBar(
    modifier: Modifier = Modifier,
    columnSpecs: List<ColumnSpec<T, *>>,
    filters: List<ColumnFilter<T, *>>,
    onFilterConfirm: (columnFilter: ColumnFilter<T, *>) -> Unit,
    onRemoveFilter: (ColumnFilter<T, *>) -> Unit
) {

    var showFilterPicker by remember { mutableStateOf(false) }
    var showFilterSetup by remember { mutableStateOf(false) }
    var selectedColumnSpec: ColumnSpec<T, *>? by remember { mutableStateOf(null) }

    Row(modifier) {
        IconButton(onClick = { showFilterPicker = !showFilterPicker }) {
            Image(imageVector = DataTableIcons.FilterList, contentDescription = "Filter")
        }
        DropdownMenu(showFilterPicker, onDismissRequest = { showFilterPicker = false }) {
            columnSpecs.forEach { columnSpec ->
                DropdownMenuItem(
                    text = {
                        Text(columnSpec.headerName)
                    },
                    {
                    selectedColumnSpec = columnSpec; showFilterPicker = false; showFilterSetup =
                    true
                })
            }
        }
        // Dropdown menu used as a Popup because it's simpler to use and has animations
        DropdownMenu(
            showFilterSetup,
            modifier = Modifier.padding(16.dp),
            onDismissRequest = { showFilterSetup = false }) {
            Text(selectedColumnSpec?.headerName ?: "", style = MaterialTheme.typography.headlineSmall)

            when (selectedColumnSpec!!) {
                is TextColumnSpec -> TextFilterModal(
                    selectedColumnSpec as TextColumnSpec<T>,
                    onFilterConfirm = { columnFilter ->
                        showFilterSetup = false
                        showFilterPicker = false
                        onFilterConfirm(columnFilter)
                    },
                    onClose = { showFilterSetup = false }
                )

                is IntColumnSpec -> IntFilterModal(
                    selectedColumnSpec as IntColumnSpec<T>,
                    onFilterConfirm = { columnFilter ->
                        showFilterSetup = false
                        showFilterPicker = false
                        onFilterConfirm(columnFilter)
                    },
                    onClose = { showFilterSetup = false }
                )

                is DoubleColumnSpec -> DoubleFilterModal(
                    selectedColumnSpec as DoubleColumnSpec<T>,
                    onFilterConfirm = { columnFilter ->
                        showFilterSetup = false
                        showFilterPicker = false
                        onFilterConfirm(columnFilter)
                    },
                    onClose = { showFilterSetup = false }
                )

                is CheckboxColumnSpec -> CheckboxFilterModal(
                    selectedColumnSpec as CheckboxColumnSpec<T>,
                    onFilterConfirm = { columnFilter ->
                        showFilterSetup = false
                        showFilterPicker = false
                        onFilterConfirm(columnFilter)
                    },
                    onClose = { showFilterSetup = false }
                )

                is DateColumnSpec -> DateFilterModal(
                    selectedColumnSpec as DateColumnSpec<T>,
                    onFilterConfirm = { columnFilter ->
                        showFilterSetup = false
                        showFilterPicker = false
                        onFilterConfirm(columnFilter)
                    },
                    onClose = { showFilterSetup = false }
                )
                is DropdownColumnSpec -> DropdownFilterModal(
                    selectedColumnSpec as DropdownColumnSpec<T, *>,
                    onFilterConfirm = { columnFilter ->
                        showFilterSetup = false
                        showFilterPicker = false
                        onFilterConfirm(columnFilter)
                    },
                    onClose = { showFilterSetup = false }
                )
            }
        }

        filters.forEach {
            InputChip(
                true,
                modifier = Modifier.padding(end = 8.dp),
                onClick = { onRemoveFilter(it) },
                label = { Text(it.label) },
                colors = InputChipDefaults.inputChipColors(
                    containerColor = Color.Transparent,
                    selectedContainerColor = Color.Transparent,
                    labelColor = MaterialTheme.colorScheme.onSurface,
                    selectedLabelColor = MaterialTheme.colorScheme.onSurface
                ),
                border = InputChipDefaults.inputChipBorder(
                    borderColor = Color(0x1f000000),
                    borderWidth = 1.dp,
                    selected = false,
                    enabled = true
                ),
                trailingIcon = {
                    IconButton(
                        modifier = Modifier.size(18.dp),
                        onClick = { onRemoveFilter(it) }
                    ) {
                        Image(imageVector = DataTableIcons.Close, contentDescription = "Filter")
                    }
                }

            )
        }
    }
}